package com.bkhech.bigdata.spark.akka;

import akka.actor.typed.Behavior;
import akka.actor.typed.javadsl.AbstractBehavior;
import akka.actor.typed.javadsl.ActorContext;
import akka.actor.typed.javadsl.Behaviors;
import akka.actor.typed.javadsl.Receive;
import lombok.extern.slf4j.Slf4j;

/**
 *
 * @author guowm
 * @date 2022/7/11
 */
@Slf4j
public class HelloActor extends AbstractBehavior<String> {
    /**
     * 常规操作：私有化构造方法
     * @param context
     */
    public HelloActor(ActorContext<String> context) {
        super(context);
    }

    /**
     * 常规操作：通过create()静态方法返回经过封装的Behavior
     * @return
     */
    public static Behavior<String> create() {
//        return Behaviors.setup(context -> new HelloActor(context));
        return Behaviors.setup(HelloActor::new);
    }

    @Override
    public Receive<String> createReceive() {
        return newReceiveBuilder()
                .onMessage(String.class, this::onMessage)
                .build();
    }

    /**
     * 收到消息之后，会进入此方法，即在此方法中实现处理消息的逻辑
     * @param msg
     * @return
     */
    private Behavior<String> onMessage(String msg) {
        log.info("HelloActor receive a message: {}", msg);
        return this;
    }
}
